/************************************************************
* author: alexander.aivars <alexander.aivars&#64;svt.se>
* $Date:2008-01-18 16:34:37 +0100 (Fri, 18 Jan 2008) $
* $Revision:211 $
* $HeadURL:svn://stoiis03.internt.svt.se/SVTiFLASH/trunk/SVTSE/Source/Classes/se/svt/controls/Button.as $
* $Id:Button.as 211 2008-01-18 15:34:37Z alai $
************************************************************/
package se.svt.controls
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import se.svt.events.ButtonEvent;
	
	/**
	* Dispatched when a button is .
	*
	* @eventType se.svt.events.ButtonEventUp
	*/
	[Event(name="ButtonEventUp", type="se.svt.events.ButtonEvent")]

	/**
	* Dispatched when a button is .
	*
	* @eventType se.svt.events.ButtonEventDown
	*/
	[Event(name="ButtonEventDown", type="se.svt.events.ButtonEvent")]

	/**
	* Dispatched when a button is .
	*
	* @eventType se.svt.events.ButtonEventOver
	*/
	[Event(name="ButtonEventOver", type="se.svt.events.ButtonEvent")]

	/**
	* Dispatched when a button is .
	*
	* @eventType se.svt.events.ButtonEventOut
	*/
	[Event(name="ButtonEventOut", type="se.svt.events.ButtonEvent")]

	/**
	* Dispatched when a button is .
	*
	* @eventType se.svt.events.ButtonEventPress
	*/
	[Event(name="ButtonEventPress", type="se.svt.events.ButtonEvent")]

	/**
	* Dispatched when a button is .
	*
	* @eventType se.svt.events.ButtonEventDisabled
	*/
	[Event(name="ButtonEventDisabled", type="se.svt.events.ButtonEvent")]


	public class Button extends MovieClip
	{

		public static const OVER:String = "over";
		public static const NORMAL:String = "normal";
		public static const DOWN:String = "down";
		public static const DISABLED:String = "disabled";

		public function Button()
		{
			super();
			this.buttonMode = true;
			addEventListener(Event.ADDED_TO_STAGE,addToStage);
			addEventListener(Event.RENDER, setup);
		}

		protected function addToStage(event:Object):void {
			
			removeEventListener(Event.ADDED_TO_STAGE,addToStage);

			addEventListener(MouseEvent.ROLL_OVER,overState);
			addEventListener(MouseEvent.ROLL_OUT,outState);
			addEventListener(MouseEvent.MOUSE_DOWN,downState);
			addEventListener(MouseEvent.MOUSE_UP,upState);
			addEventListener(MouseEvent.CLICK,clickState);

			stage.invalidate();
		}

		protected function setup(obj:Object):void {
			removeEventListener(Event.RENDER, setup);
			gotoAndStop(Button.NORMAL);
			this.stop();
		}
		
		protected function outState(event:MouseEvent):void {
			stage.removeEventListener(MouseEvent.MOUSE_UP,outState);
			if (event.currentTarget == this) {
				gotoAndStop(Button.NORMAL);
				dispatchEvent( new ButtonEvent(ButtonEvent.OUT,true,true) );
			}
		}

		protected function upState(event:MouseEvent):void {
			if (event.currentTarget == this) {
				gotoAndStop(Button.NORMAL);
				dispatchEvent( new ButtonEvent(ButtonEvent.UP,true,true) );
			}
		}

		protected function overState(event:MouseEvent):void {
			stage.addEventListener(MouseEvent.MOUSE_UP,outState);
			if (event.currentTarget == this) {
				gotoAndPlay(Button.OVER);
				dispatchEvent( new ButtonEvent(ButtonEvent.OVER,true,true) );
			}
		}

		protected function downState(event:MouseEvent):void {
			if (event.currentTarget == this) {
				gotoAndStop(Button.DOWN);
				dispatchEvent( new ButtonEvent(ButtonEvent.DOWN,true,true) );
			}
		}

		protected function disabledState(event:MouseEvent):void {
			if (event.currentTarget == this) {
				gotoAndPlay(Button.DISABLED);
				dispatchEvent( new ButtonEvent(ButtonEvent.DISABLED,true,true) );
			}
		}

		protected function clickState(event:MouseEvent):void {
			if (event.currentTarget == this) {
				dispatchEvent( new ButtonEvent(ButtonEvent.PRESS,true,true) );
			}
		}
	}
}